package com.glory.dgcpcpda.activity.mfg;

import androidx.recyclerview.widget.RecyclerView;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.LotShelvesLotInfoAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.LotList;
import com.glory.dgcpcpda.entity.PackageEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class LotUpSuggestActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.rvLotList)
    RecyclerView rvLotList;
    @BindView(R.id.etShelves)
    FreeUI_ClearEditText etShelves;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private LotShelvesLotInfoAdapter mLotAdapter;
    private String isBig = "";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_lot_shelves;
    }

    @Override
    protected void initView() {
        initLayout();

        initEdit(etLotID, s -> {

            //获取lot信息
            LotUtils.searchLotInfo(LotUpSuggestActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
                @Override
                public void onSuccess(LotInfoEntity response) {
                    for (LotInfoEntity entity : mLotAdapter.getData()) {
                        if (entity.getLOT().getLOTID().equals(response.getLOT().getLOTID())) {
                            showToast("当前批次已加载到列表中"+entity.getLOT().getCOMCLASS(), false);
                            return;
                        }

                    }
                    if("COM".equals(response.getLOT().getCOMCLASS().trim())){
                        showToast("当前批次已完工，不允许上架", false);
                        return;
                    }else if("FIN".equals(response.getLOT().getCOMCLASS().trim())){
                        showToast("当前批次已终止，不允许上架", false);
                        return;
                    }else if("SCHD".equals(response.getLOT().getCOMCLASS().trim())){
                        showToast("当前批次未投产，不允许上架", false);
                        return;
                    }
                    requestTestBank(response);
                }

                @Override
                public void onFail(Throwable t) {
                    loadError(t, "LOTQUERY");
                }
            });
        });
    }

    private void requestTestBank(LotInfoEntity entity) {
        FreeUI_EntityCallBack<BaseResponseBean<PackageEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<PackageEntity>>
                (new TypeToken<BaseResponseBean<PackageEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<PackageEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    String storageid = response.body().getResponse().getBody().getSTORAGEID();
                    if (getText(etShelves).equals("") || getText(etShelves).equals(storageid)) {
                        etShelves.setText(storageid);
                        mLotAdapter.addData(entity);
                        etLotID.setText("");
                        etLotID.requestFocus();
                    } else {
                        FreeApi_DialogUtils.getConfirmAndCancelDialog(LotUpSuggestActivity.this, "货架号推荐不一致是否继续!", new FreeUI_CommonCallback() {
                            @Override
                            public void onSuccess() {
                                etShelves.setText(storageid);
                                mLotAdapter.addData(entity);
                                etLotID.setText("");
                                etLotID.requestFocus();
                            }
                        }, () -> {
                        }).show(getSupportFragmentManager(), "SUBMIT_DIALOG");
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<PackageEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "TESTBANKSUGGESTLOCATOR");
            }
        };
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("ACTIONTYPE", "");
        jsonObject.addProperty("ISBIG", isBig);
        jsonObject.addProperty("LOTID", entity.getLOT().getLOTID());
        NetUtils.requestPostNet("CPC.TESTBANKSUGGESTLOCATOR", "TESTBANKSUGGESTLOCATOR", this, jsonObject, callBack);
    }

    private void initLayout() {
        tvTitle.setText("批次上架(推荐)");
        setMainLayout(llMain);
        initRecyclerView(rvLotList, this);
        mLotAdapter = new LotShelvesLotInfoAdapter(null);
        mLotAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mLotAdapter.isFirstOnly(true);
        rvLotList.setAdapter(mLotAdapter);

    }

    @Override
    protected void initData() {
    }

    @OnClick({R.id.tvBaseSubmit,R.id.rbBig,R.id.rbSmall,R.id.rbTube})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if (mLotAdapter.getData().size() <= 0 || getText(etShelves).equals("")) {
                    showToast("批次列表或货架号不能为空", false);
                    return;
                }

                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                                (new TypeToken<BaseResponseBean<JSONObject>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                                    showToast("上架成功", true);
                                    LotUpSuggestActivity.this.finish();
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                                super.onError(response);
                                loadError(response.getException(), "CPCMOVELOCATOR");
                            }
                        };
                        JsonObject map = new JsonObject();
                        map.addProperty("ACTIONTYPE", "BIND");
                        List<LotList> lotList = new ArrayList<>();
                        for (LotInfoEntity entity : mLotAdapter.getData()) {
                            lotList.add(new LotList(entity.getLOT().getLOTID()));
                        }
                        map.add("LOTLIST", new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());//传批次集合
                        map.addProperty("LOCATORID", getText(etShelves).toUpperCase());
                        NetUtils.requestPostNet("CPC.CPCMOVELOCATOR", "CPCMOVELOCATOR", this, map, callBack);
                    }
                });
                break;
            case R.id.rbBig:
                isBig = "大盘";
                break;
            case R.id.rbSmall:
                isBig = "小盘";
                break;
            case R.id.rbTube:
                isBig = "管装";
                break;
        }
    }
}